package com.cheetah.start.common.valid;

import cn.hutool.core.util.StrUtil;
import com.cheetah.start.common.valid.anno.NotDecimal;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

public class ValidNotDecimalValidator implements ConstraintValidator<NotDecimal, String> {


    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isBlank(value)) {
            return true;
        } else {
            try {
                String match = "^-?[0-9]\\d*$";
                return Pattern.matches(match, value);
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }

    @Override
    public void initialize(NotDecimal constraintAnnotation) {
    }
}
